package com.umeox.moto.watch.themes.widget;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.R;

public class RoundProgressBar extends View {
    public RoundProgressBar(Context context) {
        super(context);
        initView(context, null);
    }

    public RoundProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public RoundProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private int showWidth = 0;
    private int rpbBackground = 0;
    private int rpbProgressColor = 0;
    private int progress = 0;
    private int max = 0;
    private int realWidth = 0;
    private int realHeight = 0;
    private final Path backgroundPath = new Path();
    private final Path progressPath = new Path();
    private final PathMeasure backgroundPathMeasure = new PathMeasure(backgroundPath, true);

    private final Paint paint = new Paint();

    public void setProgress(int value) {
        progress = value;
        invalidate();
    }

    public void setProgress(int value, int progressMax) {
        progress = value;
        max = progressMax;
        invalidate();
    }

    public void setProgressMax(int value) {
        max = value;
        invalidate();
    }

    public void setProgressColor(int color) {
        rpbProgressColor = color;
        invalidate();
    }

    public void setProgressBgColor(int color) {
        rpbBackground = color;
        invalidate();
    }

    @SuppressLint("ResourceType")
    private void initView(Context context, AttributeSet attrs) {
        if (attrs != null && context != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
            showWidth =
                    a.getDimensionPixelSize(R.styleable.RoundProgressBar_showWidth, getDefaultShowWidth(context));
            rpbBackground =
                    a.getColor(R.styleable.RoundProgressBar_rpbBackground, Color.parseColor("#FFFFFF"));
            rpbProgressColor = a.getColor(
                    R.styleable.RoundProgressBar_rpbProgressColor,
                    Color.parseColor("#000000")
            );
            progress = a.getInt(R.styleable.RoundProgressBar_rpbProgress, 0);
            max = a.getInt(R.styleable.RoundProgressBar_rpbMax, 100);
            a.recycle();
        }

        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        realWidth = w;
        realHeight = h;
        float radius = Math.min(realWidth, realHeight) / 2.0f;
        RectF rectF = new RectF(
                showWidth / 2.0f,
                showWidth / 2.0f,
                realWidth - showWidth / 2.0f,
                realHeight - showWidth / 2.0f
        );
        backgroundPath.reset();
        backgroundPath.addRoundRect(rectF, radius, radius, Path.Direction.CCW);
        backgroundPathMeasure.setPath(backgroundPath, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.rotate(90.0f, realWidth / 2.0f, realHeight / 2.0f);
        paint.setStrokeWidth(showWidth * 1.0f);
        paint.setColor(rpbBackground);
        canvas.drawPath(backgroundPath, paint);

        paint.setColor(rpbProgressColor);
        progressPath.reset();
        backgroundPathMeasure.getSegment(
                (1.0f - (progress * 1.0f / max)) * backgroundPathMeasure.getLength(),
                backgroundPathMeasure.getLength(),
                progressPath,
                true
        );
        canvas.drawPath(progressPath, paint);
    }

    private int getDefaultShowWidth(Context context) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP,
                30,
                context.getResources().getDisplayMetrics()
        );
    }

}
